package com.mall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Brand;
import com.mall.db.domain.Category;
import com.mall.db.domain.Goods;
import com.mall.db.service.BrandService;
import com.mall.db.service.CategoryService;
import com.mall.db.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxd
 * @since 2021-09-22
 */
@Controller
@RequestMapping("/goods/gc")
public class GoodsController extends BaseController<Goods>{

    @Resource(name = "goodsService")
    private GoodsService gs;

    @Resource(name = "brandService")
    private BrandService bs;

    @Resource(name = "categoryService")
    private CategoryService cs;


    @RequestMapping("/brandClass")
    public Map<String, Object> branClass(Brand brand, ModelMap modelMap){

        QueryWrapper<Brand> qw = new QueryWrapper<>();
        List<Brand> list  = bs.list(qw);
        modelMap.addAttribute("list",list);
        return this.iud("查询",true,list);
    }

    @RequestMapping("/category")
    public JSONArray category(Category category, ModelMap modelMap){

        QueryWrapper<Category> qw = new QueryWrapper<Category>();

        List<Category> list = cs.list(qw);
        return JSONArray.parseArray(JSON.toJSONString(list));
    }



    @Override

    public Map<String, Object> find(Goods goods) {

        //实现分页
        PageHelper.startPage(goods.getPage(),goods.getLimit());

        QueryWrapper<Goods> qw = new QueryWrapper<>();

        if (!StringUtils.isEmpty(goods.getName())){
            qw.eq("name",goods.getName());
        }

        if (!StringUtils.isEmpty(goods.getCategoryId())){
            qw.eq("category_id",goods.getCategoryId());
        }

        List<Goods> list = gs.list(qw);

        PageInfo<Goods> page = new PageInfo<>(list,goods.getLimit());


        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> add(Goods goods) {
        boolean b = gs.save(goods);
        return this.iud("新增",b,goods);
    }

    @Override
    public Map<String, Object> update(Goods goods) {

        boolean b = gs.updateById(goods);
        return this.iud("编辑",b,goods);
    }

/*    @Override
    public Map<String, Object> delete(Goods goods) {

        Map<String, Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(goods.getId())){
            gs.removeById(goods.getId());
            map.put("is",true);
            map.put("msg","删除成功！");
        }else {
            map.put("is",false);
            map.put("msg","删除失败！");
        }

        return map;
    }*/


    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Map<String,Object> batchUpdate(@RequestParam(value = "ids[]") List<Integer> ids){

        Map<String,Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(ids)){
            gs.removeByIds(ids);
            map.put("is",true);
            map.put("msg","删除成功！");
        }

        else {
            map.put("is",false);
            map.put("msg","删除失败！");
        }

        return map;
    }
}

